let xg_data_e = []
let xg_data_i = []
let xg_data_s = []
let xg_data_n = []
let xg_data_t = []
let xg_data_f = []
let xg_data_j = []
let xg_data_p = []
let xg_types = {}
let xg_type = ''


if (uni.getStorageSync('questionDesc')) {
	xg_data_e = uni.getStorageSync('questionDesc').XG.xg_data_e
	xg_data_i = uni.getStorageSync('questionDesc').XG.xg_data_i
	xg_data_s = uni.getStorageSync('questionDesc').XG.xg_data_s
	xg_data_n = uni.getStorageSync('questionDesc').XG.xg_data_n
	xg_data_t = uni.getStorageSync('questionDesc').XG.xg_data_t
	xg_data_f = uni.getStorageSync('questionDesc').XG.xg_data_f
	xg_data_j = uni.getStorageSync('questionDesc').XG.xg_data_j
	xg_data_p = uni.getStorageSync('questionDesc').XG.xg_data_p
	xg_types = uni.getStorageSync('questionDesc').XG.xg_types
}


// 2 - 10
// 1轻微(2-4) 2中等(5-7) 3明显(8-9)   相对4强(9-10)

// function formatXG1(v) {
// 	if (v < 5) {
// 		return 1;
// 	} else if (v < 8) {
// 		return 2;
// 	} else {
// 		return 3;
// 	}
// }

function formatXG(xg1, xg2) {
	// let _v1 = formatXG1(xg1.v);
	// let _v2 = formatXG1(xg2.v);
	// if (_v1 == 3 && _v2 == 1) {
	// 	_v1 = 4;
	// } else
	// if (_v1 == 1 && _v2 == 3) {
	// 	_v2 = 4;
	// }

	const xg = (xg1.v >= xg2.v) ? xg1 : xg2;
	return {
		'xg': xg,
		'v1': xg1.v,
		'v2': xg2.v,
	};
}

const group = ['ISTJ', 'ISFJ', 'ISTP', 'ISFP', 'ESTP', 'ESFP', 'ESTJ', 'ESFJ', 'INFJ', 'INTJ', 'INFP', 'INTP', 'ENFP',
	'ENTP', 'ENFJ', 'ENTJ'
]
/**
 * 性格前四项排序
 * @param {Object} items
 */
function combinatorialSort(res) {
	const xgList = res.map(item => item.d.t);

	for (const item of group) {
		const sortedXgList = xgList.slice().sort();
		const sortedTargetElement = item.split('').sort();

		if (sortedXgList.join('') == sortedTargetElement.join('')) {
			const new_xgList = []
			// 获取性格匹配后描述
			xg_type = xg_types[item]

			item.split('').forEach((char, i) => {
				const index = xgList.findIndex(xg => xg === char);
				new_xgList[index] = res[i];
			});
			return new_xgList.length === 4 ? new_xgList : res;
		}
	}

	return res
}

//    XG 性格维度（MBTI测试）
//    XG_E 外向（外倾）- XG_I 内向（内倾）   
//    XG_S 感觉 - XG_N 直觉 
//    XG_T 思维（思考）- XG_F 情感
//    XG_J 判断 - XG_P 知觉     每种性格2道题共16道题
function getBaogaoXG(data) {
	if (Object.keys(data).length != 8) {
		return;
	}

	if (
		data.E == undefined ||
		data.I == undefined ||
		data.S == undefined ||
		data.N == undefined ||
		data.T == undefined ||
		data.F == undefined ||
		data.J == undefined ||
		data.P == undefined
	) {
		return
	}

	const xg_e = {
		'v': data.E,
		'd': xg_data_e
	};
	const xg_i = {
		'v': data.I,
		'd': xg_data_i
	};

	const xg_s = {
		'v': data.S,
		'd': xg_data_s
	};
	const xg_n = {
		'v': data.N,
		'd': xg_data_n
	};

	const xg_t = {
		'v': data.T,
		'd': xg_data_t
	};
	const xg_f = {
		'v': data.F,
		'd': xg_data_f
	};

	const xg_j = {
		'v': data.J,
		'd': xg_data_j
	};
	const xg_p = {
		'v': data.P,
		'd': xg_data_p
	};
	const vs1 = formatXG(xg_e, xg_i);
	const vs2 = formatXG(xg_s, xg_n);
	const vs3 = formatXG(xg_t, xg_f);
	const vs4 = formatXG(xg_j, xg_p);

	const xg1 = vs1.xg;
	const xg2 = vs2.xg;
	const xg3 = vs3.xg;
	const xg4 = vs4.xg;
	let xgList = [xg1, xg2, xg3, xg4]
	console.log('xgList', xgList)

	// xgList.sort((a, b) => b.v - a.v)
	xgList = combinatorialSort(xgList)
	console.log('xgList', xgList)

	const t1 = xgList[0].d.t;
	const t2 = xgList[1].d.t;
	const t3 = xgList[2].d.t;
	const t4 = xgList[3].d.t;

	const n1 = xgList[0].d.n;
	const n2 = xgList[1].d.n;
	const n3 = xgList[2].d.n;
	const n4 = xgList[3].d.n;

	// 类型
	const xg_lx = `${t1}${t2}${t3}${t4}(${n1}+${n2}+${n3}+${n4})`;
	const ms1 = xgList[0].d.ms;
	const ms2 = xgList[1].d.ms;
	const ms3 = xgList[2].d.ms;
	const ms4 = xgList[3].d.ms;

	// 描述
	const xg_ms = [
		ms1[0] + ms1[1],
		ms2[0] + ms2[1],
		ms3[0] + ms3[1],
		ms4[0] + ms4[1]
	];

	// 八维
	let chartSdata = [
		vs1.v1,
		vs1.v2,
		vs2.v1,
		vs2.v2,
		vs3.v1,
		vs3.v2,
		vs4.v1,
		vs4.v2,
	]
	const percentages = chartSdata.map(score => {
		// if (score === 1) {
		// 	return '20%';
		// } else if (score === 2) {
		// 	return '40%';
		// } else if (score === 3) {
		// 	return '60%';
		// } else if (score === 4) {
		// 	return '80%';
		// }
		return score
	});
	const xg_bw = {
		"categories": [
			`${xg_e.d.n}(${xg_e.d.t})`,
			`${xg_i.d.n}(${xg_i.d.t})`,
			`${xg_s.d.n}(${xg_s.d.t})`,
			`${xg_n.d.n}(${xg_n.d.t})`,

			`${xg_t.d.n}(${xg_t.d.t})`,
			`${xg_f.d.n}(${xg_f.d.t})`,
			`${xg_j.d.n}(${xg_j.d.t})`,
			`${xg_p.d.n}(${xg_p.d.t})`,
		],

		"series": [{
			"format": "xgSeries",
			"name": "你的性格维度",
			"data": percentages
		}, ]
	};
	const transformData = (originalData) => {
		const listData = [];
		const {
			categories,
			series
		} = originalData;
		const data = series[0].data;

		for (let i = 0; i < categories.length; i += 2) {
			// let total = data[i] + data[i + 1]
			let total = 10
			let leftW = data[i] / total
			// let rightW = 1 - leftW
			let rightW = data[i + 1] / total

			listData.push({
				id: i / 2 + 1,
				lefttitle: categories[i],
				righttitle: categories[i + 1],
				leftlinWidth: leftW * 100, // + '%',
				rightlinWidth: rightW * 100 // + '%'
			});
		}

		return listData;
	};
	const listData = transformData(xg_bw);
	return {
		"xg_lx": xg_lx,
		"xg_ms": xg_ms,
		"xg_bw": listData,
		"xg_type": xg_type,
	};
}
export default {
	getBaogaoXG: getBaogaoXG,
}